package com.ketangpai.admin.controller;

import com.ketangpai.admin.entity.User;
import com.ketangpai.admin.entity.UserCourse;
import com.ketangpai.admin.entity.dto.CourseDTO;
import com.ketangpai.admin.entity.dto.UserCourseDTO;
import com.ketangpai.admin.entity.dto.UserGetAllClassDTO;
import com.ketangpai.admin.entity.dto.UserGetDivideClass;
import com.ketangpai.admin.entity.param.UserAddCourseParam;
import com.ketangpai.admin.entity.param.UserCourseParam;
import com.ketangpai.admin.mapper.CourseMapper;
import com.ketangpai.admin.service.UserCourseService;
import com.ketangpai.admin.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 用户与课程(UserCourse)表控制层
 *
 * @author makejava
 * @since 2023-06-14 01:14:48
 */
@RestController
@RequestMapping("userCourse")
@Api(tags = "用户课程")
public class UserCourseController {
    /**
     * 服务对象
     */
    @Resource
    private UserCourseService service;
    @Autowired
    private CourseMapper courseMapper;
    /**
     * 列表查询
     */
    @PostMapping("/list")
    @ApiOperation("查询")
    public JsonResult<?> queryByPage(@RequestBody UserCourseParam param) {
        Map<String, Object> map = service.selectList(param);
        System.out.println("map:"+map);
        return JsonResult.success(map);
    }

//    /**
//     * 根据课程id查询用户
//     */
//    @ApiOperation("根据课程id查询用户")
//    @GetMapping("/getUserByCid/{id}")
//    public JsonResult<?> getUserByCid(@PathVariable Integer id) {
//        Map<String, Object> map = service.getUserByCid(id);
//        return JsonResult.success(map);
//    }

    /**
     * 根据课程id和类型查询用户
     */
    @ApiOperation("根据课程id和类型查询用户")
    @GetMapping("/getUserByCidAndType/{id}/{type}")
    public JsonResult<?> getUserByCidAndType(@PathVariable Integer id, @PathVariable Integer type) {
        Map<String, Object> map = service.getUserByCidAndType(id, type);
        return JsonResult.success(map);
    }

    /**
     * 根据课程id查询老师
     */
    @ApiOperation("根据课程id查询老师")
    @PostMapping("/getTeacher")
    public JsonResult<?> getTeacher(@RequestBody UserCourseParam param) {
        Map<String, Object> map = service.getTeacher(param);
        return JsonResult.success(map);
    }

    @ApiOperation("根据课程id查询用户课程")
    @PostMapping("/getUserCourse")
    public JsonResult<?> getUserCourse(@RequestBody UserCourseParam param) {
        UserCourseDTO user = service.getUserCourse(param);
        return JsonResult.success(user);
    }
    @ApiOperation("根据课程id查询用户")
    @GetMapping("/getUserInfo")
    public JsonResult<?> getUserInfo(@RequestParam  Integer id) {
        User userInfo = service.getUserInfo(id);
        return JsonResult.success(userInfo,"查询成功");
    }
    @ApiOperation("根据课程id查询用户")
    @PostMapping("/getUser")
    public JsonResult<?> getUser(@RequestBody UserCourseParam param) {
        UserCourseDTO map = service.getUser(param);
        return JsonResult.success(map);
    }

    /**
     * 新增数据
     */
    @ApiOperation("添加")
    @PostMapping("/insert")
    public JsonResult<?> insert(@RequestBody UserCourse course) {
        boolean result = service.insert(course);
        if (result) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.error("添加失败");

    }
    /**
     * 新增助教
     */
    @ApiOperation("添加")
    @PostMapping("/insertAssist")
    public JsonResult<?> insertAssist(@RequestBody UserCourse course) {
        boolean result = service.insertAssist(course);
        if (result) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.error("用户已被添加");

    }
    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    public JsonResult<?> update(@RequestBody UserCourse course) {
        boolean result = service.update(course);
        if (result) {
            return JsonResult.success("编辑成功");
        }
        return JsonResult.error("编辑失败");
    }

    @ApiOperation("根据课程id归档全部用户课程")
    @GetMapping("/placeToFile/{id}")
    public JsonResult<?> placeToFile(@PathVariable Integer id) {
        boolean result = service.placeToFile(id);
        if (result) {
            return JsonResult.success("归档成功");
        }
        return JsonResult.error("归档失败");
    }

    /**
     * 搜索
     */
    @ApiOperation("根据课程名称搜索课程")
    @PostMapping("/searchNormalClass")
    public JsonResult<?> searchNormalClass(@RequestBody UserCourseParam userCourseParam) {
//        CourseDTO courseDTOList = courseMapper.searchClass(userCourseParam);
        System.out.println("userCourseParam:"+userCourseParam);
        UserGetDivideClass courseDTOList = service.searchNormalClass(userCourseParam);
        return JsonResult.success(courseDTOList);
    }
    /**
     * 搜索
     */
    @ApiOperation("根据课程名称搜索归档课程")
    @PostMapping("/searchPigeonholeClass")
    public JsonResult<?> searchClass(@RequestBody UserCourseParam userCourseParam) {
//        CourseDTO courseDTOList = courseMapper.searchClass(userCourseParam);
        UserGetDivideClass courseDTOList = service.searchPigeonholeClass(userCourseParam);
        return JsonResult.success(courseDTOList);
    }
    /**
     * 根据课程id查询归档课程
     */
    @ApiOperation("根据课程id查询归档课程")
    @GetMapping("/getPigeonholeClass")
    public JsonResult<?> getPigeonholeClass(@RequestParam Integer id) {
        UserGetDivideClass Class = service.getPigeonholeClass(id);
        return JsonResult.success(Class,"查询成功");
    }
    /**
     * 根据课程id查询top课程
     */
    @ApiOperation("根据课程id查询top课程")
    @GetMapping("/getTopClass")
    public JsonResult<?> getTop(@RequestParam Integer id) {
        List<CourseDTO> topClassById = service.getTopClassById(id);
        return JsonResult.success(topClassById,"查询成功");
    }
    /**
     * 根据课程id查询所有课程
     */
    @ApiOperation("根据课程id查询学习课程")
    @GetMapping("/getLearnClass")
    public JsonResult<?> getClass(@RequestParam Integer id) {
        List<UserGetAllClassDTO> Class = service.getLearnClass(id);
        return JsonResult.success(Class,"查询成功");

    }
    /**
     * 根据课程id查询教学课程
     */
    @ApiOperation("根据课程id查询教学课程")
    @GetMapping("/getTeachClass")
    public JsonResult<?> getTeachClass(@RequestParam Integer id) {
        List<UserGetAllClassDTO> Class = service.getTeachClass(id);
        return JsonResult.success(Class,"查询成功");

    }
    /**
     * 根据课程id查询协助课程
     */
    @ApiOperation("根据课程id查询协助课程")
    @GetMapping("/getAssistClass")
    public JsonResult<?> getAssistClass(@RequestParam Integer id) {
        List<UserGetAllClassDTO> Class = service.getAssistClass(id);
        return JsonResult.success(Class,"查询成功");

    }

    /**
     * 根据课程id归档课程
     */
    @ApiOperation("根据课程id归档课程")
    @PostMapping("/pigeonholeClass")
    public JsonResult<?> pigeonholeClass(@RequestBody UserCourseParam courseParam) {
        boolean result = service.pigeonholeClass(courseParam);
        System.out.println(courseParam);
        if (result){
            return JsonResult.success("归档成功");
        }else {
            return JsonResult.error("归档失败");

        }

    }
    /**
     * 根据课程id取消归档课程
     */
    @ApiOperation("根据课程id归档课程")
    @PostMapping("/unPigeonholeClass")
    public JsonResult<?> unPigeonholeClass(@RequestBody UserCourseParam courseParam) {
        boolean result = service.unPigeonholeClass(courseParam);
        System.out.println(courseParam);
        if (result){
            return JsonResult.success("归档成功");
        }
        return JsonResult.error("归档失败");

    }
    /**
     * 根据课程id和用户id改变置顶状态
     */
    @ApiOperation("根据课程id和用户id改变置顶状态")
    @PostMapping("unTop")
    public JsonResult<?> unTop(@RequestBody UserCourseParam courseParam) {
        boolean result = service.unTopById(courseParam);
        System.out.println(courseParam);
        if (result){
            return JsonResult.success("删除置顶成功");
        }
        return JsonResult.error("删除失败");
    }
    /**
     * 根据课程id和用户id改变置顶状态
     */
    @ApiOperation("根据课程id和用户id改变置顶状态")
    @PostMapping("top")
    public JsonResult<?> Top(@RequestBody UserCourseParam courseParam) {
        boolean result = service.topById(courseParam);
        System.out.println(courseParam);
        if (result){
            return JsonResult.success("置顶成功");
        }
        return JsonResult.error("置顶失败");
    }
    /**
     * 退课
     */
    @ApiOperation("删除学生和课程关系")
    @PostMapping("/deleteClass")
    public JsonResult<?> delete(@RequestBody UserCourseParam courseParam) {
        System.out.println(courseParam);
        boolean result = service.deleteById(courseParam);
        if (result) {
            return JsonResult.success("删除成功");
        }else {
            return JsonResult.error("删除失败");
        }

    }
    /**
     * 添加课程
     */
    @ApiOperation("添加课程")
    @PostMapping("/addClass")
    public JsonResult<?> addClass(@RequestBody UserAddCourseParam userAddCourseParam) {

        JsonResult<?> result = service.addClass(userAddCourseParam);
        if (result.getCode()==200) {
            return JsonResult.success("添加成功");
        }else if (result.getMessage()=="课程已添加"){
            return JsonResult.error("添加失败,用户已添加该课程");
        }else if (result.getMessage()=="课程不存在"){
            return JsonResult.error("添加失败,课程不存在");
        }else
            return JsonResult.error("添加失败");
    }
    /**
     * 创建课程
     */
    @ApiOperation("创建课程")
    @PostMapping("/createClass")
    public JsonResult<?> createClass(@RequestBody CourseDTO course) {
        Boolean result = service.createClass(course);
        if (result) {
            return JsonResult.success("添加成功");
        }else {
            return JsonResult.error("添加失败");
        }

    }
    /**
     * 编辑课程
     */
    @ApiOperation("编辑课程")
    @PostMapping("/editClass")
    public JsonResult<?> editClass(@RequestBody CourseDTO course) {
        Boolean result = service.editClass(course);
        if (result) {
            return JsonResult.success("编辑成功");
        }else {
            return JsonResult.error("编辑失败");
        }

    }
    /**
     * 删除课程
     */
    @ApiOperation("删除课程")
    @PostMapping("/deleteTeachClass")
    public JsonResult<?> deleteTeachClass(@RequestBody UserCourseParam courseParam) {
        boolean result = service.deleteTeachById(courseParam);
        boolean result1 = courseMapper.deleteById(courseParam.getCourseId());
        if (result && result1) {
            return JsonResult.success("删除成功");
        }else {
            return JsonResult.error("删除失败");
        }

    }
    @ApiOperation("修正课程成员数量")
    @GetMapping("/updateClassCount")
    public JsonResult<?> updateClassCount() {
        service.getStuNum();
return JsonResult.success();
    }
    @ApiOperation("获取学生成员数量")
    @PostMapping("/getStudent")
    public JsonResult<?> getStudent(@RequestBody UserCourseParam courseParam) {
        List<User> students = service.getStudent(courseParam);
        return JsonResult.success(students);
    }
}


